package com.zcf.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.common.result.ResultVo;
import com.zcf.mapper.TaocanMapper;
import com.zcf.mapper.TaocanpMapper;
import com.zcf.pojo.Taocan;
import com.zcf.pojo.Taocanp;
import com.zcf.service.TaocanService;
import com.zcf.utils.UploadImgUtils;
import com.zcf.vo.in.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Controller
@RequestMapping("/taocan")
@CrossOrigin
@ResponseBody
public class TaocanController {

    @Autowired
    private TaocanService taocanService;


    @Autowired
    private TaocanpMapper taocanpMapper;
    // 上传套餐
    @PostMapping("/insertimg")
    public Body insertimg(MultipartFile[] file) {
        if (file != null && !"".equals(file)) {
            try {
                String fan = UploadImgUtils.uploadFiles(file);
                return Body.newInstance(fan);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return Body.newInstance(501, "照片不能为空");

    }

    // 添加套餐
    @PostMapping("/addTaocan")
    public Body addTaocan(String name, String content, String dinid, String sale, String zksale, int xh, String img) {
        return taocanService.addTaocan(name, content, dinid, sale, zksale, xh, img);
    }

    // 添加套餐内容
    @PostMapping("/addTaocanp")
    public Body addTaocanp(String price, String pid, String tid) {
        return taocanService.addTaocanp(price, pid, tid);
    }

    // 修改套餐
    @PostMapping("/upTaocan")
    public Body upTaocan(String id, String name, String content, String dinid, String sale, String zksale, int xh, String img) {
        return taocanService.upTaocan(id, name, content, dinid, sale, zksale, xh, img);
    }

    // 删除套餐
    @PostMapping("/deletTaocan")
    public Body deletTaocan(String id) {
        Taocan taocan = new Taocan();
        taocan.setId(id);
        boolean b = taocan.deleteById();
        if (b) {
            return Body.newInstance(200, "删除成功");
        }
        return Body.newInstance(501, "删除失败");
    }
    // 删除套餐产品
    @PostMapping("/deletTaocanp")
    public Body deletTaocanp(String id) {
        Taocanp taocanp = new Taocanp();
        taocanp.setId(id);
        boolean b = taocanp.deleteById();
        if (b) {
            return Body.newInstance(200, "删除成功");
        }
        return Body.newInstance(501, "删除失败");
    }

    /**
     * 查看用户默认套餐
     */
    @PostMapping("query")
    public Body query(String id) {
        Taocan taocan = new Taocan();
        EntityWrapper<Taocan> ew = new EntityWrapper<>();
        ew.eq("id", id);
        taocan = taocan.selectOne(ew);
        if (taocan != null) {
            return Body.newInstance(taocan);
        }
        return Body.newInstance(-1, "没有查到");
    }

    // 套餐列表
    @PostMapping("/taocanList")
    public Body taocanList(PageVo pv, String userId) {
        ResultVo rv = taocanService.taocanList(pv, userId);
        return Body.newInstance(rv);
    }

    // 套餐列表
    @PostMapping("/taocanListAll")
    public Body taocanListAll() {
        return taocanService.taocanListAll();
    }

    // 产品列表
    @PostMapping("/taocanpList")
    public Body taocanpList() {
        List<Taocanp> list = taocanpMapper.TaocanpList();
        if (!list.isEmpty()) {
            return Body.newInstance(list);
        }
        return Body.newInstance(501, "目前没有产品");
    }

}
